package org.zfes.snowier.cloud.zbss.metting.biz.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.metting.biz.service.IMettingsAttachesService;
import org.zfes.snowier.cloud.zbss.metting.biz.service.IMettingsService;
import org.zfes.snowier.cloud.zbss.metting.biz.service.ISignupModelService;
import org.zfes.snowier.cloud.zbss.metting.biz.dao.MettingsMapper;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.MettingDto;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.MettingsAttachesCreateDto;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.MettingsCreateDto;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.MettingsUpdateDto;
import org.zfes.snowier.cloud.zbss.metting.biz.model.Mettings;
import org.zfes.snowier.cloud.zbss.metting.biz.model.MettingsAttaches;
import org.zfes.snowier.cloud.zbss.metting.biz.model.SignupModel;

import java.util.stream.Stream;
@Service
public class MettingsServiceImpl implements IMettingsService{
 	@Autowired
	private MettingsMapper mettingsMapper;
	
	@Autowired
	private IMettingsAttachesService mettingsAttachesService;
	
	@Autowired
	private  ISignupModelService signupModelService;
	
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createMettings(MettingsCreateDto mettingsCreateDto) {
		ZBeanUtil.validateBean(mettingsCreateDto);
		
		Mettings mettings=new Mettings().setCreatorUserId(mettingsCreateDto.getCreatorUserId())
				.setCreatorUserName(mettingsCreateDto.getCreatorUserName());
		
		boolean isAfter=ZDateUtil.isAfter( mettingsCreateDto.getMetStartTime(),mettingsCreateDto.getMetEndTime());
		if(isAfter) {
			ZAlert.newSLE("会议开始时间不能晚于会议结束时间");
		}
		
		boolean isAfter2=ZDateUtil.isAfter(mettingsCreateDto.getSignupStartTime(), mettingsCreateDto.getSignupEndTime());
		if(isAfter2) {
			ZAlert.newSLE("报名开始时间不能晚于报名结束时间");
		}
		
		boolean isAfter3=ZDateUtil.isAfter(mettingsCreateDto.getSignupEndTime(), mettingsCreateDto.getMetStartTime());
		if(!isAfter3) {
			ZAlert.newSLE("报名开始时间不能晚于会议开始时间");
		}
		
		ZBeanUtil.copy(mettingsCreateDto,mettings, true);
		
		ZBeanUtil.setCreateInfo(mettings);
		ZBeanUtil.validateBean(mettings);
		mettingsMapper.insertSelective(mettings);
		Long metId=mettings.getId();
		
		//附件
		if(ZStrUtil.hasText(mettingsCreateDto.getAttachIds())) {
			
			String[] attIdArray=mettingsCreateDto.getAttachIds().split(",");
			String[] attNamesArray=mettingsCreateDto.getAttachNames().split(",");
			
			for( int i=0; i< attIdArray.length; i++) {
				mettingsAttachesService
				.createMettingsAttaches(new MettingsAttachesCreateDto( metId, attIdArray[i], attNamesArray[i]));
			}
			
		}
		//报名模板
		//signupModelService.createSignupModel(new SignupModel("["+mettingsCreateDto.getTitle()+"]模板",  metId));
		
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Mettings> loadMettingsById(Long id) {
		if(id!=null){
			return Optional.ofNullable(mettingsMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<MettingDto> loadMettingsDtoById(Long id) {
		if(id!=null){
			Mettings mettings=mettingsMapper.selectByPrimaryKey(id);
			if(mettings==null) {
				return Optional.empty();
			}
			MettingDto mettingDto=new MettingDto().setMettings(mettings);
			
			List<MettingsAttaches> attachList = mettingsAttachesService.loadAttachesByMetId(id, Byte.valueOf("1"));
			if(ZObjectUtil.isNotEmpty(attachList)) {
				
				mettingDto.setAttachList(attachList);
			}
			return Optional.ofNullable(mettingDto);
		}
		return Optional.empty();
		
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateMettings(MettingsUpdateDto mettingsUpdateDto) {
		ZBeanUtil.validateBean(mettingsUpdateDto);
		boolean isAfter=ZDateUtil.isAfter( mettingsUpdateDto.getMetStartTime(),mettingsUpdateDto.getMetEndTime());
		if(isAfter) {
			ZAlert.newSLE("会议开始时间不能晚于会议结束时间");
		}
		
		boolean isAfter2=ZDateUtil.isAfter(mettingsUpdateDto.getSignupStartTime(), mettingsUpdateDto.getSignupEndTime());
		if(isAfter2) {
			ZAlert.newSLE("报名开始时间不能晚于报名结束时间");
		}
		
		boolean isAfter3=ZDateUtil.isAfter(mettingsUpdateDto.getSignupEndTime(), mettingsUpdateDto.getMetStartTime());
		if(!isAfter3) {
			ZAlert.newSLE("报名开始时间不能晚于会议开始时间");
		}
		Mettings mettings=loadMettingsById(mettingsUpdateDto.getId()).orElseThrow(()-> ZAlert.newSLE("未查询数据信息"));
		
		ZBeanUtil.copy(mettingsUpdateDto,mettings, true);
		
		ZBeanUtil.setUpdatedInfo(mettings);
		ZBeanUtil.validateBean(mettings);
		mettingsMapper.updateByPrimaryKeySelective(mettings);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setTitle(Long id,String title) {
		ZAssert.notNull(id, "请选择要设置的数据");
		ZAssert.hasText(title, "标题不能为空");
		mettingsMapper.setTitle(id, title);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMetLogo(Long id,String metLogo) {
		ZAssert.notNull(id, "请选择要设置的数据");
		ZAssert.hasText(metLogo, "LOGO不能为空");
		mettingsMapper.setMetLogo(id, metLogo);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMetBigLogo(Long id,String metBigLogo) {
		ZAssert.notNull(id, "请选择要设置的数据");
		ZAssert.hasText(metBigLogo, "LOGO不能为空");
		mettingsMapper.setMetBigLogo(id, metBigLogo);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMetTime(Long id,Date metStartTime,Date metEndTime) {
		ZAssert.notNull(id, "请选择要设置的数据");
		ZAssert.notNull(metStartTime,"会议开始时间不能为空");
		ZAssert.notNull(metEndTime,"会议结束时间不能为空");
		if(ZDateUtil.isAfter(metStartTime, metEndTime)) {
			ZAlert.newSLE("开始时间不能晚于结束时间");
		}
		mettingsMapper.setMetTime(id, metStartTime,metEndTime);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setSignupTime(Long id,Date signupStartTime,Date signupEndTime) {
		ZAssert.notNull(id, "请选择要设置的数据");
		ZAssert.notNull(signupStartTime,"报名开始时间不能为空");
		ZAssert.notNull(signupEndTime,"报名结束时间不能为空");
		if(ZDateUtil.isAfter(signupStartTime, signupEndTime)) {
			ZAlert.newSLE("开始时间不能晚于结束时间");
		}
		mettingsMapper.setSignupTime(id, signupStartTime,signupEndTime);
	}
 	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMetDetail(Long id,String metDetail) {
		ZAssert.notNull(id, "请选择要设置的数据");
		ZAssert.hasText(metDetail, "请填写详情");
		mettingsMapper.setMetDetail(id, metDetail);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMetAgenda(Long id,String metAgenda) {
		ZAssert.notNull(id, "请选择要设置的数据");
		ZAssert.hasText(metAgenda, "请填写会议日程");
		mettingsMapper.setMetAgenda(id, metAgenda);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMetNotes(Long id,String metNotes) {
		ZAssert.notNull(id, "请选择要设置的数据");
		ZAssert.hasText(metNotes, "请填写会议须知");
		mettingsMapper.setMetNotes(id, metNotes);
	}
 	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateSignupCount(Long id,int countTobeAdd) {
		ZAssert.notNull(id, "请选择要设置的数据");
		ZAssert.bigOrEqThanZero(countTobeAdd, "参数错误", "参数错误");
		mettingsMapper.updateSignupCount(id, countTobeAdd);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMetLinkInfo(Long id,String metLinkInfo) {
		ZAssert.notNull(id, "请选择要设置的数据");
		ZAssert.hasText(metLinkInfo, "请填写联系人信息");
		mettingsMapper.updateMetLinkInfo(id, metLinkInfo);
	}
//#1报名中，2报名结束，3会议进行中，4会议结束，5取消-1移除 #	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMettingsStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,1,2,3,4,5}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		mettingsMapper.updateStatus( id,  status) ;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadMettingsDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(mettingsMapper.selectListCount(pm), mettingsMapper.selectMapListPage(pm));
		return ds;
	}
	
}